package com.asset.dispoal.business.repayment.service.strategy;

import com.asset.dispoal.business.bsCase.domain.db.entity.BsCase;
import com.asset.dispoal.business.casedispose.domain.db.mapper.CasedisposeMapper;
import com.asset.dispoal.business.repayment.domain.db.entity.BsCaseRepayment;
import com.asset.dispoal.business.repayment.domain.db.mapper.CaseRepaymentMapper;
import com.asset.dispoal.business.repayment.dto.RepaymentInfoResponse;
import com.asset.dispoal.commom.menus.AgreementCaseSateMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @description: 判决 回显信息全部按照判决信息处理
 * @author: yf
 * @date: 2022-09-28 15:17:11
 */
@Component("judgmentState")
public class JudgmentCase implements IRepaymentStrategy {
    @Autowired
    private CasedisposeMapper casedisposeMapper;
    @Autowired
    private CaseRepaymentMapper caseRepaymentMapper;

    @Override
    public RepaymentInfoResponse getRepaymentInfo(BsCase bsCase) {
        //还款信息是否存在
        BsCaseRepayment caseRepayment = caseRepaymentMapper.queryByCaseId(bsCase.getId(), bsCase.getCaseState());
        AgreementCaseSateMenu caseSateMenu = AgreementCaseSateMenu.getByLabel(bsCase.getCaseState());
        if (null != caseRepayment) {
            return RepaymentInfoResponse.builder()
                    .repaymentId(caseRepayment.getId())
                    .caseId(bsCase.getId())
                    .caseState(bsCase.getCaseState())
                    .caseStateName(caseSateMenu.getValue())
                    .caseStateName(caseSateMenu.getValue())
                    .lawsuitPrincipalAmount(nullValue(caseRepayment.getPrincipalAmount()))
                    .lawsuitInterestAmount(nullValue(caseRepayment.getInterestAmount()))
                    .litigationFines(nullValue(caseRepayment.getLitigationFines()))
                    .litigationAmount(nullValue(caseRepayment.getLitigationAmount()))
                    .preservationAmount(nullValue(caseRepayment.getPreservationAmount()))
                    .toDoReturnPrincipalAmount(nullValue(bsCase.getPrincipal()))
                    .toDoInterestAmount(nullValue(bsCase.getInterest()))
                    .toDoLitigationFines(nullValue(bsCase.getPenaltyInterest()))
                    .toDoLitigationAmount(nullValue(bsCase.getCaseAcceptAmount()))
                    .todoPreservationAmount(nullValue(bsCase.getJudgmentPreservationAmount()))
                    .repaymentPrincipal(nullValue(caseRepayment.getRepaymentPrincipal()))
                    .repaymentInterest(nullValue(caseRepayment.getRepaymentInterest()))
                    .repaymentPenalty(nullValue(caseRepayment.getRepaymentPenalty()))
                    .repaymentLitigationAmount(nullValue(caseRepayment.getRepaymentLitigationAmount()))
                    .repaymentPreservationAmount(nullValue(caseRepayment.getRepaymentPreservationAmount()))
                    .build();
        }


        return RepaymentInfoResponse.builder()
                .caseId(bsCase.getId())
                .caseState(bsCase.getCaseState())
                .caseStateName(caseSateMenu.getValue())
                .lawsuitPrincipalAmount(bsCase.getPrincipal().toPlainString())
                .lawsuitInterestAmount(bsCase.getInterest().toPlainString())
                .litigationFines(bsCase.getPenaltyInterest().toPlainString())
                .litigationAmount(bsCase.getCaseAcceptAmount().toPlainString())
                .preservationAmount(bsCase.getJudgmentPreservationAmount().toString())
                .toDoReturnPrincipalAmount(bsCase.getPrincipal().toPlainString())
                .toDoInterestAmount(bsCase.getInterest().toPlainString())
                .toDoLitigationFines(bsCase.getPenaltyInterest().toPlainString())
                .toDoLitigationAmount(bsCase.getCaseAcceptAmount().toPlainString())
                .todoPreservationAmount(bsCase.getJudgmentPreservationAmount().toString())
                .repaymentPrincipal(BigDecimal.ZERO.toPlainString())
                .repaymentInterest(BigDecimal.ZERO.toPlainString())
                .repaymentPenalty(BigDecimal.ZERO.toPlainString())
                .repaymentLitigationAmount(BigDecimal.ZERO.toPlainString())
                .repaymentPreservationAmount(BigDecimal.ZERO.toPlainString())
                .litigationAmount(bsCase.getCaseAcceptAmount().toPlainString())
                .preservationAmount(bsCase.getJudgmentPreservationAmount().toPlainString())
                .build();
    }

    private String nullValue(BigDecimal bigDecimal) {
        return bigDecimal == null ? BigDecimal.ZERO.toPlainString() : bigDecimal.toPlainString();
    }


//    /**
//     * 判决列表计算
//     * @param bsCase
//     * @return
//     */
//    private BigDecimal calPenaltyInterest(BsCase bsCase) {
//        List<JudgmentDto> judgments = getJudgments(bsCase);
//
//        BigDecimal penaltyInterestAmount = BigDecimal.ZERO;
//        for (JudgmentDto judgmentDto : judgments) {
//            if("固定".equals(judgmentDto.getJudgmentType())){
//                BigDecimal calculate = calculate(judgmentDto.getJudgmentCalculationTime(),null,  judgmentDto.getBaseNumber(), judgmentDto.getJudgmentRate().toString(), judgmentDto.getJudgmentDay());
//                penaltyInterestAmount = penaltyInterestAmount.add(calculate == null ? BigDecimal.ZERO : calculate);
//            }
//            else {
//                CaseDisposeFormulaDTO caseDisposeFormulaDTO = casedisposeMapper.getFormulaDataById(judgmentDto.getJudgmentRate());
//                for (CaseDisposeFormulaDetailDTO item : caseDisposeFormulaDTO.getDetailList()) {
//                    BigDecimal calculate = calculate(judgmentDto.getJudgmentCalculationTime(), item, judgmentDto.getBaseNumber(), null, null);
//                    penaltyInterestAmount = penaltyInterestAmount.add(calculate == null ? BigDecimal.ZERO : calculate);
//                }
//            }
//        }
//        return penaltyInterestAmount.setScale(0, BigDecimal.ROUND_HALF_UP);
//    }
//
//    /**
//     * 列表计算
//     * @param judgmentCalculationTime
//     * @param item
//     * @param baseNumber
//     * @param judgmentRate
//     * @param judgmentDay
//     * @return
//     */
//    private BigDecimal calculate(Date judgmentCalculationTime, CaseDisposeFormulaDetailDTO item, BigDecimal baseNumber, String judgmentRate, Integer judgmentDay) {
//        if (item != null) {
//            //如果计算的时间大于开始时间则进行处理，如果不大于开始时间则不允许进行运算
//            if(judgmentCalculationTime.after(item.getStartTime())){
//                //如果计算时间大于结束时间
//                if(judgmentCalculationTime.after(item.getEndTime())){
//                    judgmentCalculationTime = item.getEndTime();
//                }
//                //取得范围天数
//                long diffDay = DateTimeUtil.getLocalDateDiffDay(DateTimeUtil.dateToLocalDate(item.getStartTime()),DateTimeUtil.dateToLocalDate(judgmentCalculationTime));
//                return baseNumber.multiply(BigDecimal.valueOf(diffDay)).multiply(item.getBigRate()).setScale(2, BigDecimal.ROUND_HALF_UP);
//            }
//            return BigDecimal.ZERO;
//        } else {
//            long diffDay = DateTimeUtil.getLocalDateDiffDay(DateTimeUtil.dateToLocalDate(judgmentCalculationTime), LocalDate.now());
//            return baseNumber.multiply(BigDecimal.valueOf(diffDay)).multiply(new BigDecimal(judgmentRate)).divide(judgmentDay == null ? BigDecimal.ONE : BigDecimal.valueOf(judgmentDay), 2, BigDecimal.ROUND_HALF_UP);
//        }
//    }
//
//    private List<JudgmentDto> getJudgments(BsCase bsCase) {
//        ArrayList<JudgmentDto> judgmentDtos = new ArrayList<>(5);
//        if (null != bsCase.getJudgmentType1()) {
//            JudgmentDto judgmentDto1 = new JudgmentDto();
//            judgmentDto1.setBaseNumber(bsCase.getBaseNumber1());
//            judgmentDto1.setJudgmentType(bsCase.getJudgmentType1());
//            judgmentDto1.setJudgmentRate(bsCase.getJudgmentRate1());
//            judgmentDto1.setJudgmentCalculationTime(bsCase.getJudgmentCalculationTime1());
//            judgmentDto1.setJudgmentDay(bsCase.getJudgmentDay1());
//            judgmentDtos.add(judgmentDto1);
//        }
//        if (null != bsCase.getJudgmentType2()) {
//            JudgmentDto judgmentDto2 = new JudgmentDto();
//            judgmentDto2.setBaseNumber(bsCase.getBaseNumber2());
//            judgmentDto2.setJudgmentType(bsCase.getJudgmentType2());
//            judgmentDto2.setJudgmentRate(bsCase.getJudgmentRate2());
//            judgmentDto2.setJudgmentCalculationTime(bsCase.getJudgmentCalculationTime2());
//            judgmentDto2.setJudgmentDay(bsCase.getJudgmentDay2());
//            judgmentDtos.add(judgmentDto2);
//        }
//        if (null != bsCase.getJudgmentType3()) {
//            JudgmentDto judgmentDto3 = new JudgmentDto();
//            judgmentDto3.setBaseNumber(bsCase.getBaseNumber3());
//            judgmentDto3.setJudgmentType(bsCase.getJudgmentType3());
//            judgmentDto3.setJudgmentRate(bsCase.getJudgmentRate3());
//            judgmentDto3.setJudgmentCalculationTime(bsCase.getJudgmentCalculationTime3());
//            judgmentDto3.setJudgmentDay(bsCase.getJudgmentDay3());
//            judgmentDtos.add(judgmentDto3);
//        }
//        if (null != bsCase.getJudgmentType4()) {
//            JudgmentDto judgmentDto4 = new JudgmentDto();
//            judgmentDto4.setBaseNumber(bsCase.getBaseNumber4());
//            judgmentDto4.setJudgmentType(bsCase.getJudgmentType4());
//            judgmentDto4.setJudgmentRate(bsCase.getJudgmentRate4());
//            judgmentDto4.setJudgmentCalculationTime(bsCase.getJudgmentCalculationTime4());
//            judgmentDto4.setJudgmentDay(bsCase.getJudgmentDay1());
//            judgmentDtos.add(judgmentDto4);
//        }
//        if (null != bsCase.getJudgmentType5()) {
//            JudgmentDto judgmentDto5 = new JudgmentDto();
//            judgmentDto5.setBaseNumber(bsCase.getBaseNumber5());
//            judgmentDto5.setJudgmentType(bsCase.getJudgmentType5());
//            judgmentDto5.setJudgmentRate(bsCase.getJudgmentRate5());
//            judgmentDto5.setJudgmentCalculationTime(bsCase.getJudgmentCalculationTime5());
//            judgmentDto5.setJudgmentDay(bsCase.getJudgmentDay5());
//            judgmentDtos.add(judgmentDto5);
//        }
//        return judgmentDtos;
//    }

}
